<template>
  <div>
    <el-dialog
      v-if="dialogVisible"
      v-model="dialogVisible"
      :before-close="handleClose"
      :title="title"
      width="35%"
    >
      <el-form label-position="left" label-width="100px">
        <el-form-item :label="translate('彩屏标识')">
          <el-input v-model="data.UUID" disabled style="width: 215px" />
        </el-form-item>
        <el-form-item :label="translate('内部编码')">
          <el-input v-model="data.Code" style="width: 215px" @blur="checkCs" />
        </el-form-item>
        <el-form-item :label="translate('组合编码')">
          <el-input v-model="data.CSCode" disabled style="width: 215px" />
        </el-form-item>
        <el-form-item :label="translate('正反面')">
          <!-- <el-input v-model="data.CSName" style="width: 350px" /> -->
          <el-select v-model="data.CSName" class="m-2" placeholder="Select">
            <el-option
              v-for="item in csNames"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="translate('屏幕类型')">
          <el-select v-model="data.LCDType" placeholder="select">
            <el-option
              v-for="item in lcdTypes"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="translate('商品列表')">
          <!-- <el-input v-model="data.Version" style="width: 350px" /> -->
          <template v-if="selectedItems && selectedItems.length">
            <!-- <span
              v-for="(item, index) in selectedItems"
              :key="item.Code"
              class="item"
            >
              {{ item.Name }}
              <span v-if="index < selectedItems.length - 1">；</span>
            </span> -->
            <el-tag
              v-for="tag in selectedItems"
              :key="tag.Code"
              class="mx-1"
              closable
              @close="tagClose(tag)"
            >
              <span style="font-size: 14px">{{ tag.Name }}</span>
            </el-tag>
          </template>
          <template
            v-if="!selectedItems || (selectedItems && selectedItems.length < 4)"
          >
            <el-button style="margin-left: 8px" @click="addPro">
              {{ translate('添加商品') }}
            </el-button>
          </template>
        </el-form-item>
        <el-form-item :label="translate('彩屏模板')">
          <el-select v-model="data.CSTypeID" placeholder="select">
            <el-option
              v-for="item in typeList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="translate('版本号')">
          <el-input v-model="data.Version" style="width: 215px" />
        </el-form-item>
        <!-- <el-form-item label="备注">
          <el-input v-model="data.Remark" style="width: 350px" />
        </el-form-item> -->
        <el-form-item :label="translate('状态')">
          <el-switch
            v-model="data.SendStatus"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="handleClose">{{ translate('取消') }}</el-button>
          <el-button type="primary" @click="confirm">
            {{ translate('确定') }}
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
  <itemDialog
    :selected-items="selectedItems"
    :title="translate('编辑')"
    :visible="itemDialogVisible"
    @close="itemDialogHandleClose"
    @confirm="itemDialogHandleConfirm"
  />
</template>

<script lang="ts" setup>
  import { ref } from 'vue'
  import { getCsPageData, checkcss, getmuprolist } from '@/api/mupro'
  import itemDialog from './itemDialog.vue'
  import { translate } from '@/i18n'
  const props = defineProps({
    visible: {
      type: Boolean,
      default: false,
    },
    data: {
      type: Object,
      default() {
        return {}
      },
    },
    title: {
      type: String,
      default: translate('编辑'),
    },
  })
  const lcdTypes = ref([
    {
      label: '600*1024',
      value: '00',
    },
    {
      label: '800*1280',
      value: '03',
    },
  ])
  const csNames = [
    {
      label: translate('全部'),
      value: '',
    },
    {
      label: translate('正面'),
      value: '正面',
    },
    {
      label: translate('反面'),
      value: '反面',
    },
    {
      label: translate('正反面'),
      value: '正反面',
    },
  ]
  const dialogVisible = ref(false)
  const data: any = ref(JSON.parse(JSON.stringify(props.data)))
  watch(
    () => props.visible,
    () => {
      dialogVisible.value = props.visible
      if (props.visible) {
        data.value = JSON.parse(JSON.stringify(props.data))
        if (props.data.MPCode) {
          getproList()
        } else {
          selectedItems.value = []
        }
      }
    }
  )
  const emit = defineEmits(['close', 'confirm'])
  const handleClose = () => {
    dialogVisible.value = false
    emit('close', dialogVisible.value)
  }
  const confirm = () => {
    data.value.MPCode = selectedItems.value
      .map((item: any) => {
        return item.Code
      })
      .join(',')
    emit('confirm', data.value)
    // dialogVisible.value = false
  }

  const typeList: any = ref([])
  const pageData: any = ref([])
  const getPageData = async () => {
    const params = {
      pageNo: 1,
      pageSize: 20,
      tableName: '',
      sort: '',
      order: '',
      wheres: ``,
      export: true,
      value: '',
    }
    const { data } = await getCsPageData(params)
    pageData.value = data?.rows || []
    typeList.value = pageData.value.map((item: any) => {
      return {
        value: item.CSTCode,
        label: item.CSTName,
      }
    })
  }
  getPageData()
  // 添加商品
  const itemDialogVisible = ref(false)
  const selectedItems: any = ref([])
  const addPro = () => {
    itemDialogVisible.value = true
  }
  const itemDialogHandleClose = () => {
    itemDialogVisible.value = false
  }
  const itemDialogHandleConfirm = (result: any) => {
    selectedItems.value = result || []
    // if (result.length) {
    //   data.value.MPCode = result
    //     .map((item: any) => {
    //       return item.Code
    //     })
    //     .join(',')
    // }
    itemDialogVisible.value = false
  }
  // 检验编码
  const checkCs = () => {}
  // 移除商品
  const tagClose = (tag: any) => {
    selectedItems.value = selectedItems.value.filter((item: any) => {
      if (item.Code !== tag.Code) {
        return item
      }
    })
  }
  // 获取组合商品明细
  const proList = ref([])
  const getproList = async () => {
    const { data: list } = await getmuprolist({
      mpCode: props.data.MPCode,
    })
    proList.value = [].concat(list)
    selectedItems.value = proList.value.map((item: any) => {
      return {
        Code: item.pCode,
        Name: item.pName,
      }
    })
  }
  // 根据选中商品数量过滤模板
  watch(
    () => selectedItems,
    () => {
      if (selectedItems.value.length && pageData.value.length) {
        typeList.value = pageData.value
          .filter((item: any) => {
            if (
              item.ProNum === selectedItems.value.length ||
              item.ProNum === 99
            ) {
              return item
            }
          })
          .map((item: any) => {
            return {
              value: item.CSTCode,
              label: item.CSTName,
            }
          })
        const values = typeList.value.map((item: any) => item.value)
        if (values.indexOf(data.value.CSTypeID) === -1) {
          data.value.CSTypeID = typeList.value[0].value
        }
      } else {
        typeList.value = pageData.value.map((item: any) => {
          return {
            value: item.CSTCode,
            label: item.CSTName,
          }
        })
      }
    },
    { deep: true }
  )
</script>

<style lang="scss" scoped>
  .tag {
    font-size: 14px;
  }
</style>
